$(document).ready(function() {
    $(".deleteProductFromCart").live("click", function () {
        document.location = Routing.generate("index")+'cart/delete/'+$(this).attr("id")+'_';
    });
    
    $(".addProductQuantity").live("click", function () {
        document.location = Routing.generate("index")+'cart/add/'+$(this).attr("id");
    });

    $(".reduceProductQuantity").live("click", function () {
        document.location = Routing.generate("index")+'cart/delete/'+$(this).attr("id");
    }); 
    
    $(".deliveryAddressRadio").live("click", function () {
        
        if($(this).is(':checked'))
        {
            var newchaine = '';
            if($('.validateAddressChoice').val() != undefined)
            {
               var chaine = $('.validateAddressChoice').val();
               var exploded = chaine.split('.');
               for(i in exploded)
               {
                   if(exploded[i] != "")
                   {
                        if(exploded[i].indexOf("_") == 0)
                        {
                            var newI = "_"+$(this).attr("value");
                            newchaine+=newI;
                        }
                        else if(exploded[i].indexOf("_") > 0)
                        {
                            var array = exploded[i].split('_')
                            newchaine = '_'+array[0]+'.'+array[1];
                        }
                        else 
                            newchaine+='.'+exploded[i];
                   } 
                   else if(exploded[i] == "" && newchaine.indexOf('_') == -1)
                        newchaine+='_'+exploded[i];
               }
               if(newchaine.indexOf('.') == -1)
                   newchaine+='.';
            }
            newchaine = newchaine.replace('..','.');
            newchaine = newchaine.replace('__','_');
            $('.validateAddressChoice').val(newchaine);
        }
    });
    
    $(".paymentAddressRadio").live("click", function () {
        if($(this).is(':checked'))
        {
            var newchaine = '';
            if($('.validateAddressChoice').val() != undefined)
            {
               var chaine = $('.validateAddressChoice').val();
               var exploded = chaine.split('_');
               
               for(i in exploded)
               {
                   if(exploded[i] != "")
                   {
                        if(exploded[i].indexOf(".") == 0)
                        {
                            var newI = "."+$(this).attr("value");
                            newchaine+=newI;
                        } else if(exploded[i].indexOf(".") > 0)
                            {
                                var array = exploded[i].split('.')
                                newchaine = '_'+array[0]+'.'+$(this).attr("value");
                            }
                        else {
                            newchaine+='_'+$(this).attr("value");
                        } 
                   }
                   else if(exploded[i] == "" && newchaine.indexOf('.') == -1) {
                       newchaine+='.'+exploded[i];
                   }
               }
               if(newchaine.indexOf('_') == -1)
                   newchaine+='_';
       
            }
            newchaine = newchaine.replace('..','.');
            newchaine = newchaine.replace('__','_');   
            $('.validateAddressChoice').val(newchaine);
        }

    });

    $(".confirmCommand").live("click", function () {
        var answer = confirm ("Vous allez valider la commande. Etes-vous sûr ?")
        if (answer)
            document.location = Routing.generate("index")+'cart/5/';
    }); 



});